Plotting Datasets

Gerd Duscher

09/24/2020

Please download this example and run it as a notebook by scrolling to the bottom of this page

[1]:
# Ensure python 3 compatibility:
from __future__ import division, print_function, absolute_import, unicode_literals
%pylab notebook

import sys
sys.path.append('../../')
import sidpy

print(sidpy.__version__)
Populating the interactive namespace from numpy and matplotlib
0.0.3

Plotting an Image

First, we make a sidpy dataset from a numpy array

[24]:
x = np.random.normal(3, 2.5, size=(512, 512))
dset = sidpy.Dataset.from_array(x)

Next, we add some information about this dataset

[25]:
dset.data_type = 'image'
dset.units = 'counts'
dset.quantity = 'intensity'
dset.title = 'random'

For plotting it is important to set the dimensions correctly.

[26]:
dset.set_dimension(0, sidpy.Dimension(np.arange(dset.shape[0])*.02, 'x'))
dset.x.dimension_type = 'spatial'
dset.x.units = 'nm'
dset.x.quantity = 'distance'
dset.set_dimension(1, sidpy.Dimension(np.arange(dset.shape[1])*.02, 'y'))
dset.y.dimension_type = 'spatial'
dset.y.units = 'nm'
dset.y.quantity = 'distance'

Now we plot the dataset:

[27]:
dset.plot()

Plotting a Complex Image

What if the data form a complex image?

Because, we do not want to start all over again, we will use the like_data function, which copies all metadata onto the new dataset.

The resulting plot consists of two images, which share however the axes, try it out and zoom into one image.

Another feature of the plot function is that you can add any matplotlib keywords and values to the plot.

[28]:

x = x + np.random.normal(3, 2.5, size=(512, 512)) *1j
dset_complex  = dset.like_data(x, 'complex image')
dset_complex.plot(figsize=(8,4))

Plotting a spectrum

A spectrum can also easily be populated with the apropriete metadata.

[9]:
x = np.random.normal(3, 2.5, size=(1024))
dset = sidpy.Dataset.from_array(x)

# dataset metadata
dset.data_type = 'spectrum'
dset.title = 'random'
dset.quantity = 'intensity'
dset.units = 'a.u.'

# dimension with metadata
scale = .5
offset = 390
dset.set_dimension(0, sidpy.Dimension(np.arange(dset.shape[0])*scale+offset, 'energy'))
dset.dim_0.dimension_type = 'spectral'
dset.energy.units = 'eV'
dset.energy.quantity = 'energy'

dset.plot()

Creating an Image-Stack DataSet

In the following we will make a numpy which resembles a stack of images

In the sidpy Dataset will set the data_type to image_stack for the plotting routine to know how to plot this dataset.

The dimensions have to contain at least two spatial dimensions and one that is identifiable as a stack dimension (‘stack, ‘frame’, ‘time’). First we make a stack of images

[11]:
x = np.random.normal(3, 2.5, size=(25, 512, 512))

dset = sidpy.Dataset.from_array(x)
dset.data_type = 'image_stack'
dset.units = 'counts'
dset.quantity = 'intensity'

dset.set_dimension(0, sidpy.Dimension(np.arange(dset.shape[0]), 'frame'))
dset.frame.dimension_type = 'temporal'
dset.set_dimension(1, sidpy.Dimension(np.arange(dset.shape[1])*.02, 'x'))
dset.x.dimension_type = 'spatial'
dset.x.units = 'nm'
dset.x.quantity = 'distance'
dset.set_dimension(2, sidpy.Dimension(np.arange(dset.shape[2])*.02, 'y'))
dset.y.dimension_type = 'spatial'
dset.y.units = 'nm'
dset.y.quantity = 'distance'

Plotting the Dataset

Please note that the scroll wheel will move you through the stack, also the slider and the play button will let you navigate through this image stack.

Zoom to an area and let it play!

Click on the Average button and then click on it again.

[12]:
dset.plot()

The kwargs dictionary is used to plot the image stack in TEM style with scale bar

[13]:
kwargs = {'scale_bar': True, 'cmap': 'hot'}  # or maby 'cmap': 'gray'

dset.plot(verbose=True, **kwargs)
Shape of dataset is:  (25, 512, 512)
3D dataset

Plot Dataset as Spectral Image

We need to change the data_type of the dataset to spectral_image and the dimension_type of one dimension to spectral.

Now the plot function plots it as a spectrum image.

Select the spectrum with the mouse (left click).

[15]:
dset.data_type = 'spectral_image'
dset.set_dimension(0, sidpy.Dimension(np.arange(dset.shape[0]),'spectrum'))
dset.spectrum.dimension_type = 'spectral'

dset.plot()

We make the selection more visible by setting the binning of the spectra selection.

The binning avrages over the binning box.

Run the code-cell below and look in the plot above.

[16]:
dset.view.set_bin([20, 20])

plt.show()

The axes (and figure) instances of matplotlib can be accessed throught the view attribute of the sidpy dataset.

The code cell below will draw a red square lattice on the plot above

[17]:
x, y = np.mgrid[0:501:100, 0:501:100] + 5
dset.view.axes[0].scatter(x, y, color='red');

The plotting routine can also be used independently.

Please note, that a reference (here the variable view) must be maintained for interactive plotting.

[19]:
kwargs = {'scale_bar': True, 'cmap': 'hot'}
dset.set_dimension(0, sidpy.Dimension(np.arange(dset.shape[0]),'frame'))
dset.frame.dimension_type = 'temporal'

view = sidpy.viz.dataset_viz.ImageStackVisualizer(dset, **kwargs)

In the same way as above, we can plot the dataset as an image.

Please note, that we did not have to set the data_type of the dataset.

[20]:
print(dset.shape)
kwargs = {'scale_bar': True, 'cmap': 'hot'}
view = sidpy.viz.dataset_viz.ImageVisualizer(dset, image_number=5, **kwargs)
(25, 512, 512)
[ ]: